package com.sokal.easycare.modules.payment.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.payment.entity.TransactionRecord;
import com.sokal.easycare.modules.payment.service.TransactionRecordService;

/** 
* 提现审核
* @author lihaipei
* @date 2017年5月26日 下午2:45:57 
*/
@Controller
@RequestMapping(value = "${adminPath}/payment/transactionRecord")
public class TransactionRecordController extends BaseController {

	@Autowired
	private TransactionRecordService transactionRecordService;

	@ModelAttribute
	public TransactionRecord get(@RequestParam(required=false) String id) {
		TransactionRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = transactionRecordService.get(id);
		}
		if (entity == null){
			entity = new TransactionRecord();
		}
		return entity;
	}

	@RequiresPermissions("payment:transactionRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(TransactionRecord transactionRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TransactionRecord> page = transactionRecordService.findPage(new Page<TransactionRecord>(request, response), transactionRecord); 
		model.addAttribute("page", page);
		return "modules/payment/transactionRecordList";
	}

	@RequiresPermissions("payment:transactionRecord:view")
	@RequestMapping(value = "form")
	public String form(String flag, TransactionRecord transactionRecord, Model model) {
		model.addAttribute("transactionRecord", transactionRecord);
		if ("edit".equals(flag)) {
			return "modules/payment/transactionRecordForm";
		} else {
			return "modules/payment/transactionRecordInfo";
		}
	}

	@RequiresPermissions("payment:transactionRecord:edit")
	@RequestMapping(value = "save")
	public String save(TransactionRecord transactionRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, transactionRecord)){
			return form("edit", transactionRecord, model);
		}
		TransactionRecord record = transactionRecordService.get(transactionRecord);
		record.setStatus("2");
		record.setComments(transactionRecord.getComments());
		transactionRecordService.save(record);
		addMessage(redirectAttributes, "处理提现审核成功");
		return "redirect:"+Global.getAdminPath()+"/payment/transactionRecord/?repage";
	}
	
}
